local gPeen = CreateFrame("Button", "gPeen", UIParent)
local gPeen_Text = gPeen:CreateFontString("gPeen_Text", "OVERLAY")

local _G = getfenv(0)
local bit_bor = _G.bit.bor
local COMBATLOG_FILTER_ME = COMBATLOG_FILTER_ME

local hit, crit = 0, 0
local lastHit, lastCrit = 0, 0
local spellHit, spellCrit

local font = "Interface\\Addons\\pMinimap\\media\\font.ttf"
local size = 12
local channel = "GUILD"

local function getHighest()
	local db = gPeenDB
	local hit, crit = 0, 0
	local lastHit, lastCrit = 0, 0
	local spellHit, spellCrit
	for spell in pairs(db) do
		if db[spell].normal > lastHit then
			hit = db[spell].normal
			lastHit = hit
			spellHit = spell
		end
		
		if db[spell].critical > lastCrit then
			crit = db[spell].critical
			lastCrit = crit
			spellCrit = spell
		end
	end
	return hit, crit, spellHit, spellCrit
end

local function BuffPeen(spellName, amount, critical)
	assert(type(spellName) == "string", format("bad argument #1 to 'BuffPeen' (string expected, got %s)", type(spellName)))
	assert(type(amount) == "number", format("bad argument #2 to 'BuffPeen' (number expected, got %s)", type(amount)))
	
	local db = gPeenDB

	if not db[spellName] then
		db[spellName] = {
			normal = 0,
			critical = 0,
		}
	end

	if critical then
		if amount > db[spellName].critical then
			db[spellName].critical = amount
			--gPeen:AddMessage(spellName.." : "..amount.." (CRIT)", 1, 1, 1, 1, 2)
			if MikSBT and not MikSBT.IsModDisabled() then
				MikSBT.DisplayMessage(spellName.." : "..amount.."(CRIT)", MikSBT.DISPLAYTYPE_NOTIFICATION, true, 255, 255, 255, 38, nil, nil, nil)
			else
				CombatText_AddMessage(spellName.." : "..amount.."(CRIT)", COMBAT_TEXT_SCROLL_FUNCTION, 1, 1, 1, "crit", nil)
			end
		end
	else
		if amount > db[spellName].normal then
			db[spellName].normal = amount
			--gPeen:AddMessage(spellName.." : "..amount, 1, 1, 1, 1, 2)
			if MikSBT and not MikSBT.IsModDisabled() then
				MikSBT.DisplayMessage(spellName.." : "..amount, MikSBT.DISPLAYTYPE_NOTIFICATION, true, 255, 255, 255, 38, nil, nil, nil)
			else
				CombatText_AddMessage(spellName.." : "..amount.."(CRIT)", COMBAT_TEXT_SCROLL_FUNCTION, 1, 1, 1, "crit", nil)
			end
		end
	end
	
	gPeenDB = db
	hit, crit = getHighest()
	gPeen_Text:SetText("|cffffffff"..hit.."|r/|cffffffff"..crit.."|r")
	gPeen:SetWidth(gPeen_Text:GetStringWidth())
end

local function onEnter(self)
	GameTooltip:SetOwner(self, "ANCHOR_NONE")
	GameTooltip:SetPoint("BOTTOM", self, "TOP", 0, 20)
	GameTooltip:AddDoubleLine("Spell Name", "Normal / Critical", 0, .8, 1, 0, .8, 1)
	GameTooltip:AddLine(" ")
	local db = gPeenDB
	for spell in pairs(db) do
		GameTooltip:AddDoubleLine(spell, db[spell].normal.." / "..db[spell].critical, 1, 1, 1, 1, 1, 1)
	end
	GameTooltip:AddLine("----------------------------------------------------------------------------------", 1, 1, 1)
	GameTooltip:AddLine("ALT + Right-click to reset database", 0, .8, 1)
	GameTooltip:AddLine("SHIFT + Left-click to announce highest records to "..string.lower(channel), 0, .8, 1)
	GameTooltip:Show()
end

local function onClick(self, button)
	if IsAltKeyDown() and button == "RightButton" then
		gPeenDB = {}
		gPeen_Text:SetText("|cffffffff0|r/|cffffffff0|r")
	elseif IsShiftKeyDown() and button == "LeftButton" then
		hit, crit, spellHit, spellCrit = getHighest()
		SendChatMessage("(Non-crit) "..spellHit..": "..hit, channel)
		SendChatMessage("(Crit) "..spellCrit..": "..crit, channel)
	end
end

local function onEvent(self, event, time, eventType, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellID, spellName, spellSchool, amount, school, resisted, blocked, absorbed, critical)
	if not gPeen_Text:GetText() then
		hit, crit = getHighest()
		gPeen_Text:SetText("|cffffffff"..hit.."|r/|cffffffff"..crit.."|r")
		gPeen:SetWidth(gPeen_Text:GetStringWidth())
		return
	end
	
	if eventType == "SPELL_DAMAGE" or eventType == "SPELL_PERIODIC_DAMAGE" or eventType == "SPELL_HEAL" or eventType == "SPELL_PERIODIC_HEAL" then
		if bit_bor(srcFlags, COMBATLOG_FILTER_ME) == COMBATLOG_FILTER_ME then
			if not resisted and not blocked and not absorbed then
				BuffPeen(spellName, amount, critical)
			end
		end
	end
end

gPeenDB = {}
--[[
gPeen:SetTimeVisible(2)
gPeen:SetInsertMode"TOP"
gPeen:SetFrameStrata"HIGH"
gPeen:SetHeight(60)
gPeen:SetWidth(512)
gPeen:SetPoint("TOP", UIParent, "TOP", 0, -300)
gPeen:SetFontObject(ErrorFont)
gPeen:SetFont("Fonts\\FRIZQT__.TTF", 32, "OUTLINE")
gPeen:Show()
]]

gPeen_Text:SetFont(font, size)
gPeen_Text:ClearAllPoints()
gPeen_Text:SetPoint"BOTTOMRIGHT"
gPeen_Text:SetTextColor(0, .8, 1)
gPeen_Text:Show()

gPeen:SetParent(UIParent)
gPeen:SetWidth(50)
gPeen:SetHeight(12)
gPeen:SetPoint("BOTTOMRIGHT", _G["gDPS"], "BOTTOMLEFT", -20, 0)
gPeen:SetScript("OnEnter", onEnter)
gPeen:SetScript("OnLeave", function() GameTooltip:Hide() end)
gPeen:SetScript("OnClick", onClick)
gPeen:SetScript("OnEvent", onEvent)
gPeen:RegisterForClicks"AnyUp"
gPeen:RegisterEvent"COMBAT_LOG_EVENT_UNFILTERED"
gPeen:RegisterEvent"PLAYER_LOGIN"


